<template>
  <div class="app-container">
    <div class="header">
      <div class="word">
        <div class="w1">
          用户管理
        </div>
        <div class="w2">
          在用户管理中你可以查看用户名，用户编号等基本信息，同时还可以实现查询用户信息，删除用户等功能，提供一套可行的销售管理方案
        </div>
        <div class="w2">
          用户管理系统是后台系统中重要的一环,它可以为我们提供用户的信息，为进一步销售做准备
        </div>
      </div>
      <img class="imgDri" src="../static/u=715410710,173853787&fm=30&app=106&f=PNG.png">
    </div>
    <div class="index">
      <div class="searchHead">
        <el-button type="primary" @click="dialogFormVisible = true">新增用户</el-button>
        <el-input v-model="input3" placeholder="请输入用户编号" class="searchIuput">
          <el-button slot="append" v-model="searchInf" icon="el-icon-search" @click="searchMes" />
        </el-input>
      </div>

      <el-dialog title="发出车辆信息添加" :visible.sync="dialogFormVisible">
        <el-form class="form">
          <el-form-item class="item" label="头像图片地址" :label-width="formLabelWidth">
            <el-input v-model="addMes.avater" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="用户名" :label-width="formLabelWidth">
            <el-input v-model="addMes.username" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="密码" :label-width="formLabelWidth">
            <el-input v-model="addMes.password" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="等级" :label-width="formLabelWidth">

            <el-select v-model="value" placeholder="请选择" class="in">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item class="item" label="电话号码" :label-width="formLabelWidth">
            <el-input v-model="addMes.phone" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="真实姓名" :label-width="formLabelWidth">
            <el-input v-model="addMes.realName" class="in" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addDispatchOrderLogistics()">确 定</el-button>
        </div>
      </el-dialog>

      <el-table :data="mes" type="" style="width: 100%">
        <el-table-column prop="username" label="用户名" />
        <el-table-column prop="realName" label="真实姓名" />
        <el-table-column prop="userId" label="用户id" />
        <el-table-column prop="phone" label="用户电话" />
        <el-table-column prop="gmtCreate" label="创建时间" />
        <el-table-column prop="rank" label="等级" />
        <el-table-column prop="carStatus" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="mes[scope.$index].isShow = true">修改</el-button>
            <el-dialog :modal-append-to-body="false" title="修改信息" :visible.sync="mes[scope.$index].isShow">
              <el-form class="form">
                <el-form-item class="item" label="用户名" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].username" autocomplete="off" />
                </el-form-item>
                <el-form-item class="item" label="真实姓名" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].realName" autocomplete="off" />
                </el-form-item>
                <el-form-item class="item" label="手机号" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].phone" autocomplete="off" />
                </el-form-item>
                <el-form-item class="item" label="等级" :label-width="formLabelWidth">
                  <el-select v-model="mes[scope.$index].rank" placeholder="请选择" class="in">
                    <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
                <el-form-item class="item" label="头像" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].avater" autocomplete="off" />
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="mes[scope.$index].isShow = false">取 消</el-button>
                <el-button type="primary" @click="changeDriver(scope.$index)">确 定</el-button>
              </div>
            </el-dialog>
            <el-button
              type="primary"
              :disabled="mes[scope.$index].isDelshow"
              @click="saleDel(scope.$index)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          :page-size="5"
          layout="prev, pager, next, jumper"
          :total="pageSize"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import request from '../../utils/request'
import vueQr from 'vue-qr'
export default {
  name: 'CarLogistics',
  components: {
    vueQr
  },
  data() {
    return {
      saleId: '',
      mes: [],
      dialogTableVisible: false,
      formLabelWidth: '100px',
      tranceData: 'http://localhost:9527/#/saleAll',
      addMes: {

        'avater': '',
        'gmtCreate': '',
        'gmtModified': '',
        'isDelete': 0,
        'password': '',
        'phone': '',
        'rank': 0,
        'realName': '',
        'userId': '',
        'username': ''
      },
      dialogFormVisible: false,
      pageSize: null,
      select: '',
      searchInf: '',
      input3: '',
      page: 1, options: [{
        value: '0',
        label: '普通用户'
      }, {
        value: '1',
        label: 'vip用户'
      }, {
        value: '2',
        label: '管理员'
      }
      ],
      value: ''
    }
  },
  async mounted() {
    this.logisticsOrderLogistics()
  },
  methods: {
    async saleDel(index) {
      await request({
        method: 'delete',
        url: '/mall/user/deleteById/' + this.mes[index].userId
      }).then((response) => {
        if (response.statusCode == 20000) {
          this.logisticsOrderLogistics()
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，修改失败',
            type: 'warning'
          })
        }
      })
    },
    async changeDriver(index) {
      const add = {
        'avater': this.mes[index].avater,
        'password': this.mes[index].password,
        'phone': this.mes[index].phone,
        'realName': this.mes[index].realName,
        'userId': this.mes[index].userId,
        'username': this.mes[index].username
      }
      const rankk = {
        'userId': this.mes[index].userId,
        'rank': this.mes[index].rank
      }
      console.log(add, 'add')
      await request({
        method: 'put',
        url: '/mall/user/updatewithId',
        data: add
      }).then(response => {
        if (response.statusCode == 20000) {
          this.dialogFormVisible = false
          this.addMes = {}
          this.$message({
            message: '修改成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，修改失败',
            type: 'warning'
          })
        }
      }),

      await request({
        method: 'put',
        url: '/mall/user/updateRankById',
        data: rankk
      }).then(response => {
        if (response.statusCode == 20000) {
          this.dialogFormVisible = false
          this.addMes = {}
        } else {
          this.$message({
            message: '发生错误，等级修改失败',
            type: 'warning'
          })
        }
      })
      this.logisticsOrderLogistics()
      this.mes[index].isShow = false
    },
    async logisticsOrderLogistics() {
      await request({
        method: 'get',
        url: '/mall/user/pageUser/' + this.page + '/5'
      }).then((response) => {
        console.log(response)
        this.mes = response.data.records
        for (let i = 0; i < this.mes.length; i++) {
          this.$set(this.mes[i], 'isShow', false)
          this.$set(this.mes[i], 'isDelShow', false)
          if (this.mes[i].rank == 1) {
            this.mes[i].rank = 'vip'
          } else if (this.mes[i].rank == 2) {
            this.mes[i].rank = '管理员'
          } else {
            this.mes[i].rank = '普通用户'
          }
        }
        this.pageSize = response.data.total
      })
    },
    async dispatchOrderLogisticsDel(index) {
      await request({
        method: 'delete',
        url: '/mall/user/deleteById/' + this.mes[index].userId
      }).then(response => {
        console.log(response)
        if (response.statusCode == 20000) {
          this.dialogFormVisible = false
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.logisticsOrderLogistics()
        } else {
          this.$message({
            message: '发生错误，删除失败',
            type: 'warning'
          })
        }
      })

      this.mes[index].isShowDel = false
    },
    async addDispatchOrderLogistics() {
      this.addMes.rank = this.value
      request({
        method: 'post',
        url: '/mall/user/register',
        data: this.addMes
      }).then(response => {
        console.log(response)
        if (response.statusCode == 20000) {
          this.dialogFormVisible = false
          this.logisticsOrderLogistics()
          this.addMes = {}
          this.$message({
            message: '添加成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，添加失败',
            type: 'warning'
          })
        }
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.page = val
      console.log(this.page, 'pagepage')
      request({
        method: 'get',
        url: '/mall/user/pageUser/' + this.page + '/5'
      }).then((response) => {
        console.log(response)
        this.mes = response.data.records
        for (let i = 0; i < this.mes.length; i++) {
          this.$set(this.mes[i], 'isShow', false)
          this.$set(this.mes[i], 'isDelShow', false)
          if (this.mes[i].saleStatus == '未销售') {
            this.$set(this.mes[i], 'isSaleShow', false)
          } else {
            this.$set(this.mes[i], 'isSaleShow', true)
          }
        }
        this.pageSize = response.data.total
      })
    },
    searchMes() {
      request({
        url: '/mall/user/getById/' + this.input3,
        method: 'get'
      }).then(response => {
        if (response.statusCode == 20000) {
          this.$message({
            message: '查询成功',
            type: 'success'
          })
          console.log(response)
          const q = []
          q[0] = response.data
          this.mes = q
          for (let i = 0; i < q.length; i++) {
            this.$set(q[i], 'isShow', false)
          }
          this.pageSize = 1
        } else {
          this.$message({
            message: '查无此人，查询失败',
            type: 'warning'
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.qrcode-container {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.header {
    width: 100%;
    height: 350px;
    // border: 1px solid black;
    display: flex;
    margin-bottom: 20px;
    justify-content: space-between;
}

.form {
    display: flex;
    flex-wrap: wrap;
    // justify-content: space-around;
}

.sel {
    width: 310px;
    display: flex;
    // border: 1px solid black;
    justify-content: right;
    align-content: center;
}

.sel2 {
    width: 240px;
}

.page {
    width: 100%;
    display: flex;
    justify-content: right;
}

.imgDri {
    height: 300px;
    width: 400px;
}

.item {
    width: 400px;
}

.w1 {
    height: 80px;
    font-size: 25px;
    font-weight: 350;
}

.w2 {
    height: 50px;
    font-size: 15px;
    font-weight: 250;
}

.searchHead {
    display: flex;
    justify-content: space-between;
}

.searchIuput {
    width: 350px;
}

.sel3 {
    width: 100px;
}
</style>
